package de.hsb.ai.agent.misterx;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import de.hsb.ai.agent.misterx.Drawer;


@SuppressWarnings("serial")
public class MisterXAgentGUI extends JFrame implements ItemListener {
	
	private JCheckBox _checkButton01;
	private JCheckBox _checkButton02;
	private JCheckBox _checkButton03;
	private JCheckBox _checkButton04;
	private JCheckBox _checkButton05;
	private JCheckBox _checkButton06;
	private JCheckBox _checkButton07;
	private JCheckBox _checkButton08;
	private JCheckBox _checkButton09;
	
	public MisterXAgentGUI() {
		if(Drawer.isFeatureON(Drawer.DRAW_1)) {
		this.setTitle("MisterX");
		this.init();
			
		//Event zum schliessen des JFrames (Schliesssymbol des JFrames)
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(WindowEvent winEvt) {
				System.exit(0); 
			}
		});
		}
	}

	/**
	 * initialisierung der GUI.
	 */
	private void init() {
		this.getContentPane().setBackground(Color.BLACK);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //X Fenster schliessen
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
		this.setMinimumSize(new Dimension(350, 150)); //setzt eine mögliche MinimalGröße des Fensters
		this.setLocation(screen.width/2 - this.getWidth()/2, screen.height/2- this.getHeight()/2);
		this.setResizable(false);
		this.setLayout(new FlowLayout());
		
		//JPanel erstellen
		final JPanel jPanel = new JPanel(new GridLayout(5, 1));
		
		//JPanel dem JFrame hinzufügen
		this.add(jPanel);
		
		this.startupConfig();
		this.initCheckButton(jPanel);
		
		this.setVisible(true);
	}
	
	/**
	 * definiert die Elemente die Standart-aktiviert sind.
	 */
	private void startupConfig() {
		Drawer.switchFeature(Drawer.CONVEX_HULL);
		Drawer.switchFeature(Drawer.CONVEX_HULL_CENTROID);
		//Drawer.switchFeature(Drawer.CONVEX_HULL_AREA);
		//Drawer.switchFeature(Drawer.PLAYERMOTION);
		Drawer.switchFeature(Drawer.CONNECTIONLINE);
		//Drawer.switchFeature(Drawer.CONVEX_HULL_DIRECTION);
		Drawer.switchFeature(Drawer.WALKPATH);
		//Drawer.switchFeature(Drawer.COSTS);
		//Drawer.switchFeature(Drawer.DANGERZONES);
	}
	
	/**
	 * initialisiert die Check-Buttons.
	 * @param jPanel
	 */
	private void initCheckButton(JPanel jPanel) {
		this._checkButton01 = new JCheckBox("CONVEX_HULL");
		this._checkButton01.setMnemonic(KeyEvent.VK_C); 
		this._checkButton01.setSelected(Drawer.isFeatureON(Drawer.CONVEX_HULL));
		this._checkButton01.addItemListener(this);
		this._checkButton02 = new JCheckBox("CONVEX_HULL_AREA");
		this._checkButton02.setMnemonic(KeyEvent.VK_C); 
		this._checkButton02.setSelected(Drawer.isFeatureON(Drawer.CONVEX_HULL_AREA));
		this._checkButton02.addItemListener(this);
		this._checkButton03 = new JCheckBox("CONVEX_HULL_CENTROID");
		this._checkButton03.setMnemonic(KeyEvent.VK_C); 
		this._checkButton03.setSelected(Drawer.isFeatureON(Drawer.CONVEX_HULL_CENTROID));
		this._checkButton03.addItemListener(this);
		this._checkButton04 = new JCheckBox("CONNECTIONLINE");
		this._checkButton04.setMnemonic(KeyEvent.VK_C); 
		this._checkButton04.setSelected(Drawer.isFeatureON(Drawer.CONNECTIONLINE));
		this._checkButton04.addItemListener(this);
		this._checkButton05 = new JCheckBox("COSTS");
		this._checkButton05.setMnemonic(KeyEvent.VK_C); 
		this._checkButton05.setSelected(Drawer.isFeatureON(Drawer.COSTS));
		this._checkButton05.addItemListener(this);
		this._checkButton06 = new JCheckBox("CONVEX_HULL_DIRECTION");
		this._checkButton06.setMnemonic(KeyEvent.VK_C); 
		this._checkButton06.setSelected(Drawer.isFeatureON(Drawer.CONVEX_HULL_DIRECTION));
		this._checkButton06.addItemListener(this);
		this._checkButton07 = new JCheckBox("PLAYERMOTION");
		this._checkButton07.setMnemonic(KeyEvent.VK_C); 
		this._checkButton07.setSelected(Drawer.isFeatureON(Drawer.PLAYERMOTION));
		this._checkButton07.addItemListener(this);
		this._checkButton08 = new JCheckBox("WALKPATH");
		this._checkButton08.setMnemonic(KeyEvent.VK_C); 
		this._checkButton08.setSelected(Drawer.isFeatureON(Drawer.WALKPATH));
		this._checkButton08.addItemListener(this);
		this._checkButton09 = new JCheckBox("DANGERZONES");
		this._checkButton09.setMnemonic(KeyEvent.VK_C); 
		this._checkButton09.setSelected(Drawer.isFeatureON(Drawer.DANGERZONES));
		this._checkButton09.addItemListener(this);
		
		jPanel.add(this._checkButton01);
		jPanel.add(this._checkButton02);
		jPanel.add(this._checkButton03);
		jPanel.add(this._checkButton06);
		jPanel.add(this._checkButton04);
		jPanel.add(this._checkButton05);
		jPanel.add(this._checkButton07);
		jPanel.add(this._checkButton08);
		jPanel.add(this._checkButton09);
	}

	
	/**
	 * Event des aktivieren/deaktivieren der Check-Buttons.
	 */
	@Override
	public void itemStateChanged(ItemEvent event) {
		Object source = event.getItemSelectable();
		if (source == this._checkButton01) {
			Drawer.switchFeature(Drawer.CONVEX_HULL);
			
		} else if (source == _checkButton02) {
			Drawer.switchFeature(Drawer.CONVEX_HULL_AREA);
		} else if (source == _checkButton03) {
			Drawer.switchFeature(Drawer.CONVEX_HULL_CENTROID);
		} else if (source == _checkButton04) {
			Drawer.switchFeature(Drawer.CONNECTIONLINE);  
		} else if (source == _checkButton05) {
			Drawer.switchFeature(Drawer.COSTS);
		} else if (source == _checkButton06) {
			Drawer.switchFeature(Drawer.CONVEX_HULL_DIRECTION);
		} else if (source == _checkButton07) {
			Drawer.switchFeature(Drawer.PLAYERMOTION);
		} else if (source == _checkButton08) {
			Drawer.switchFeature(Drawer.WALKPATH);
		} else if (source == _checkButton09) {
			Drawer.switchFeature(Drawer.DANGERZONES);
		}
	}

	
}

